<template>
  <div class="app-container">

    <el-card class="box-card" style="margin-bottom: 20px;border-radius: 10px;"  v-show="showSearch" >
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="最新版本号" prop="appVersionCode">
        <el-input
          v-model="queryParams.appVersionCode"
          placeholder="请输入最新版本号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="版本名称" prop="appVersionName">
        <el-input
          v-model="queryParams.appVersionName"
          placeholder="请输入最新版本名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间" prop="publishDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.publishDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择发布时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="系统平台" prop="appVersionState">
        <el-select v-model="queryParams.platform" placeholder="请选择平台" clearable size="small" style="width: 120px" @change="handleQuery">
          <el-option v-for="dict in dict.type.platform" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="使用状态" prop="appVersionState">
        <el-select v-model="queryParams.appVersionState" placeholder="请选择状态" clearable size="small" style="width: 120px" @change="handleQuery">
          <el-option v-for="dict in dict.type.app_version_state" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <!--<el-form-item label="安装包路径" prop="packagePath">
        <el-input
          v-model="queryParams.packagePath"
          placeholder="请输入安装包路径"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <!--<el-form-item label="创建时间" prop="insertTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.insertTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>

    <el-card class="box-card" style="margin-bottom: 20px;border-radius: 10px;">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['edsta:version:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['edsta:version:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['edsta:version:remove']"
        >删除</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['edsta:version:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="versionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="最新版本号" align="center" prop="appVersionCode" />
      <el-table-column label="版本名称" align="center" prop="appVersionName" />
      <el-table-column label="发布时间" align="center" prop="publishDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="系统平台" align="center" prop="platform" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.platform" :value="scope.row.platform"/>
        </template>
      </el-table-column>
      <el-table-column label="使用状态" align="center" prop="appVersionState" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_version_state" :value="scope.row.appVersionState"/>
        </template>
      </el-table-column>
      <el-table-column label="安装包路径" align="center" prop="packagePath" />
      <el-table-column label="更新公告" align="center" prop="content" >
        <template slot-scope="scope">
          <div v-html="scope.row.content"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="insertTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.insertTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['edsta:version:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['edsta:version:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>

    <!-- 添加或修改C端软件版本管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="最新版本号" prop="appVersionCode">
          <el-input v-model="form.appVersionCode" placeholder="请输入最新版本号" />
        </el-form-item>
        <el-form-item label="版本名称" prop="appVersionName">
          <el-input v-model="form.appVersionName" placeholder="请输入最先版本名称" />
        </el-form-item>
        <el-form-item label="发布时间" prop="publishDate">
          <el-date-picker clearable size="small"
            v-model="form.publishDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="系统平台" prop="platform">
          <el-select v-model="form.platform" placeholder="请选择平台" clearable size="small">
            <el-option v-for="dict in dict.type.platform" :key="dict.value" :label="dict.label"
                       :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="使用状态" prop="appVersionState">
          <el-select v-model="form.appVersionState" placeholder="请选择状态" clearable size="small">
            <el-option v-for="dict in dict.type.app_version_state" :key="dict.value" :label="dict.label"
                       :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="安装包路径" prop="packagePath">
          <el-input v-model="form.packagePath" placeholder="请输入安装包路径"></el-input>
        </el-form-item>
        <el-form-item label="更新公告">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { addVersion, delVersion, getVersion, listVersion, updateVersion } from '@/api/system/version'
  import Template from '@/views/link/productTemplate/index'
  import { parseTime } from '@/utils/edsta'

  export default {
  name: "Version",
  components: { Template },
  dicts:['app_version_state','platform','download_prefix'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      versionCodes:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // C端软件版本管理表格数据
      versionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        appVersionCode: null,
        appVersionName: null,
        publishDate: null,
        appVersionState: null,
        packagePath: null,
        content: null,
        insertTime: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        /*packagePath:{
          pattern:/(http|https):\/\/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(:[0-9]{1,5})?\/[\w\.-]+\.[\w]+/
          ,message:'请输入正确的路径',trigger:'blur'},*/
        packagePath:{required:true,pattern:/\\*.zip/,message:'当前只支持.zip文件'},
        appVersionCode:{required:true,message: '请输入版本号',trigger: 'blur'},
        publishDate:{required:true,message: '请选择发布日期',trigger: 'blur'},
        appVersionState:{required:true,message: '请选择版本状态',trigger: 'blur'},
        platform:{required:true,message: '请选择系统平台',trigger: 'blur'},
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询C端软件版本管理列表 */
    getList() {
      this.loading = true;
      listVersion(this.queryParams).then(response => {
        this.versionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        appVersionCode: null,
        appVersionName: null,
        publishDate: parseTime(new Date(),"{y}-{m}-{d}"),
        platform: 'Windows',
        appVersionState: '1',
        packagePath: this.dict.type.download_prefix[0].value,
        content: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.versionCodes = selection.map(item=>item.appVersionCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加软件版本管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVersion(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改软件版本管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVersion(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVersion(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除软件版本为"' + this.versionCodes + '"的信息吗？').then(function() {
        return delVersion(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
